import { Component, ErrorInfo, ReactNode } from 'react'
import { Alert, Button, Stack, Text } from '@mantine/core'
import { IconAlertCircle } from '@tabler/icons-react'

interface Props {
  children: ReactNode
}

interface State {
  hasError: boolean
  error: Error | null
}

export class ErrorBoundary extends Component<Props, State> {
  public state: State = {
    hasError: false,
    error: null,
  }

  public static getDerivedStateFromError(error: Error): State {
    return { hasError: true, error }
  }

  public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error('Uncaught error:', error, errorInfo)
  }

  public render() {
    if (this.state.hasError) {
      return (
        <Alert
          icon={<IconAlertCircle size={16} />}
          title="Something went wrong"
          color="red"
          variant="filled"
          my="md"
        >
          <Stack>
            <Text>
              {this.state.error?.message || 'An unexpected error occurred'}
            </Text>
            <Button
              onClick={() => this.setState({ hasError: false, error: null })}
              variant="white"
              color="red"
              size="xs"
            >
              Try again
            </Button>
          </Stack>
        </Alert>
      )
    }

    return this.props.children
  }
}
